package com.isky.visual.proj.controller;


import com.isky.visual.interceptor.PlatformUserManager;
import com.isky.visual.interceptor.annotation.LoginValidate;
import com.isky.visual.proj.entity.Project;
import com.isky.visual.proj.entity.vo.*;
import com.isky.visual.proj.service.IProjectService;
import com.isky.visual.result.CodeMsg;
import com.isky.visual.result.ResultVo;
import com.isky.visual.user.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 发布可视化项目
 * </p>
 *
 * @author wudl
 * @since 2020-04-28
 */
@Slf4j
@RestController
@RequestMapping("/proj/project")
@Api(tags = {"设计器模块"})
public class ProjectController {

    @Autowired
    IProjectService projectService;

    @PostMapping(value = "/queryMyProjectList")
    @ApiOperation("设计器 我的 （根据仪表盘组id组和仪表盘状态获取项目列表）")
    public ResultVo<List<Project>> queryMyProjectList(@RequestBody ProjectPageParam param){
        List<Project> projectList = projectService.getProjectListPage(param);
        return ResultVo.success(projectList);
    }

    @PostMapping(value = "/queryShareProjectList")
    @ApiOperation("设计器 公共 （根据仪表盘组id组和仪表盘状态获取项目列表）")
    public  ResultVo<List<Project>> queryShareProjectList(@RequestBody ProjectPageParam param){
        List<Project> projectList = projectService.queryShareProjectList(param);
        return ResultVo.success(projectList);
    }

    @LoginValidate(false)
    @GetMapping(value = "/queryById")
    @ApiOperation("通过id获取")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", required= true,dataType = "Long", paramType = "query")
    })
    public ResultVo<ProjectVO> queryById(HttpServletRequest request, @RequestParam(name = "projectId", required = false) Long projectId ){
     //   String env = (String) request.getAttribute("env");
        String env = request.getHeader("env");
        log.info("===================================:"+env);

        Project project = projectService.selectProjectById(projectId);
        if(StringUtils.isBlank(env)){
            User user = PlatformUserManager.getUser();
        }else {
           if(project == null || !Objects.equals(1,project.getStatus())){
               ResultVo.error(CodeMsg.PROJECT_NUll);
           }
        }

        ProjectVO projectVO = new ProjectVO();
        projectVO.toModel(project);
        return ResultVo.success(projectVO);
    }

    @PostMapping(value = "/publish")
    @ApiOperation("项目发布")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", required= true,dataType = "Long", paramType = "query")
      })
    public ResultVo<String> publish( @RequestParam(name = "projectId", required = false) Long projectId ){
        boolean publish = projectService.publish(projectId);
        return publish ? ResultVo.success():ResultVo.error(CodeMsg.DB_UPDATE_ERROR);
    }

    @PostMapping(value = "/delete")
    @ApiOperation("项目删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", required= true,dataType = "Long", paramType = "query")
    })
    public ResultVo<String> delete( @RequestParam(name = "projectId", required = false) Long projectId ){
        boolean update = projectService.delete(projectId);
        return update ? ResultVo.success():ResultVo.error(CodeMsg.DB_DELETE_ERROR);
    }

    @PostMapping(value = "/save")
    @ApiOperation("项目保存")
    public ResultVo<String> save( @RequestBody ProjectSaveParam projectSave ){
        ProjectVO projectVO = projectSave.getProject();
        boolean save = projectService.insertProject(projectSave.getGroupId(),projectVO.toProject());
        return save ? ResultVo.success():ResultVo.error(CodeMsg.DB_ADDDATA_ERROR);
    }
    @PostMapping(value = "/update")
    @ApiOperation("项目更新")
    public ResultVo<String> update( @RequestBody ProjectVO projectVO ){
        if (projectVO == null || projectVO.getId() == null) {
            return ResultVo.error(CodeMsg.QUERY_PARAMETER);
        }
        boolean save = projectService.updateSelect(projectVO.toProject());
        return save ? ResultVo.success():ResultVo.error(CodeMsg.DB_ADDDATA_ERROR);
    }

    @PostMapping(value = "/copy")
    @ApiOperation("项目复制")
    public ResultVo<String> copy(@RequestBody ProjectParam param){
        boolean copy = projectService.copy(param.getGroupId(), param.getProjectId());
        return copy ? ResultVo.success():ResultVo.error(CodeMsg.DATA_COPY_ERROR);
    }
    @PostMapping(value = "/move")
    @ApiOperation(value = "项目移动 ",notes="")
    public ResultVo<Object> move(@RequestBody ProjectMoveParam projectMoveParam) {
        boolean move = projectService.move(projectMoveParam);
        return move ? ResultVo.success():ResultVo.error(CodeMsg.DB_ADDDATA_ERROR);
    }

    @PostMapping(value = "/rename")
    @ApiOperation(value = "项目重命名 ",notes = "只传id和name")
    public ResultVo<Object> rename(@RequestBody ProjectVO projectVO ) {
        boolean rename = projectService.rename(projectVO.getId() ,projectVO.getProjectName());
        return rename ? ResultVo.success():ResultVo.error(CodeMsg.DB_ADDDATA_ERROR);
    }

    @PostMapping(value = "/share")
    @ApiOperation(value = "项目分享")
    public ResultVo<Object> share(@RequestBody ProjectShareParam projectShareParam ) {
        boolean share = projectService.share(projectShareParam);
        return share ? ResultVo.success():ResultVo.error(CodeMsg.DB_ADDDATA_ERROR);
    }

    @LoginValidate(false)
    @GetMapping(value = "/queryByUserGroupId")
    @ApiOperation(value = "通过用户组id查询用户组对应的页面列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户组id", required= true,dataType = "Long", paramType = "query")
    })
    public ResultVo<List<String>> queryByUserGroupId(@RequestParam(name = "id") Long id) {
        List<String> result = projectService.queryByUserGroupId(id);
        return ResultVo.success(result);
    }

    @LoginValidate(false)
    @GetMapping(value = "/queryByUserId")
    @ApiOperation(value = "通过用户id查询用户组对应的页面列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required= true,dataType = "Long", paramType = "query")
    })
    public ResultVo<List<String>> queryByUserId(@RequestParam(name = "id") Long id) {
        List<String> result = projectService.queryByUserId(id);
        return ResultVo.success(result);
    }
}
